/*
 * GameListWindow.cpp
 *
 *  Created on: 05/06/2014
 *      Author: lucio
 */

#include "GameListWindow.h"

void GameListWindow::JoinButton_clicked(){

}

void GameListWindow::CreateButton_clicked(){
	this->GameList->get_selection()->unselect_all();
	this->CreateWindow->SetNull();
	this->CreateWindow->show_all();
}

void GameListWindow::ChangePreviewImage() {
	if (*GameList->get_selection()->get_selected() != NULL) {
		Gtk::TreeModel::Row Row = *GameList->get_selection()->get_selected();
		this->PreviewImage->set(Row->get_value(model.ImagePath).data());
	}
}

void GameListWindow::CreateGame() {
	if (this->CreateWindow->getSelectedCampaingInfo().isCreated()) {
		this->selectedCampaingInfo =
				this->CreateWindow->getSelectedCampaingInfo();

		GameInfo info("1",this->CreateWindow->getGameName(),
				this->selectedCampaingInfo);
		this->games.push_back(info);
	}
}

void GameListWindow::RefreshButton_clicked(){
	int sizeGame1 = this->GameList->get_children().size();
	int sizeGame2 = this->games.size();
	if (sizeGame1 != sizeGame2) {
		this->PreviewImage->clear();
		this->ListStore->clear();
		std::vector<GameInfo>::iterator it;
		std::vector<GameInfo>::iterator begin = this->games.begin();
		std::vector<GameInfo>::iterator end = this->games.end();

		for (it = begin; it != end; ++it) {
			Gtk::TreeModel::Row row = *(this->ListStore->append());
			row[model.Id] = it->getGameID();
			row[model.Name] = it->getGameName();
			row[model.Difficult] = it->getCampaingInfo().getDifficulty();
			row[model.LevelSize] = it->getCampaingInfo().getLevelsSize();
			row[model.MinPlayers] = it->getCampaingInfo().getMinPlayers();
			row[model.ImagePath] = it->getCampaingInfo().getImageID();
		}
	}
}

void GameListWindow::CreateGameList() {
	this->ListStore = Gtk::ListStore::create(model);
	builder->get_widget("GameList", this->GameList);
	this->GameList->set_model(this->ListStore);
	this->GameList->append_column("Id", model.Id);
	this->GameList->append_column("Name", model.Name);
	this->GameList->append_column("Difficult", model.Difficult);
	this->GameList->append_column("LevelSize", model.LevelSize);
	this->GameList->append_column("MinPlayers", model.MinPlayers);
}

GameListWindow::GameListWindow(BaseObjectType* object,
		const Glib::RefPtr<Gtk::Builder> &Builder):
			Gtk::Window(object), builder(Builder)
{
	builder->get_widget("JoinButton",this->JoinButton);
	builder->get_widget("CreateButton",this->CreateButton);
	builder->get_widget("RefreshButton",this->RefreshButton);

	builder->get_widget("GameList",this->GameList);
	CreateGameList();
	builder->get_widget("PreviewImage",this->PreviewImage);

	builder->get_widget_derived("GameCreateWindow",this->CreateWindow);

	this->JoinButton->signal_clicked().connect
				(sigc::mem_fun(*this, &GameListWindow::JoinButton_clicked));

	this->CreateButton->signal_clicked().connect
				(sigc::mem_fun(*this, &GameListWindow::CreateButton_clicked));

	this->RefreshButton->signal_clicked().connect
				(sigc::mem_fun(*this, &GameListWindow::RefreshButton_clicked));

	this->GameList->signal_cursor_changed().connect
				(sigc::mem_fun(*this, &GameListWindow::ChangePreviewImage));

	this->CreateWindow->signal_hide().connect
					(sigc::mem_fun(*this, &GameListWindow::CreateGame));
}

GameListWindow::~GameListWindow() {
	// TODO Auto-generated destructor stub
}

